#!/bin/sh
# Clone_generateCtrlFileScript.sh
# takes mounted directory where image copy resides and generates datafile clause for clone controlfile creation

SID=$1
FILES=$2"/data_*"
BASEDIR=$3
DATAFILES=""

nsidlen=`echo -n $SID | wc -m`
if [ $nsidlen -gt 8 ]; then
newdb_name=`echo $SID | cut -c1-8`
else
newdb_name=$SID
fi

CLONE_CRT_SQL="Clone_createCtrlFile_"$SID"_g.sql"

if [ -e $CLONE_CRT_SQL ]; then
   echo "removing $CLONE_CRT_SQL before generating a new one"
   rm -f $CLONE_CRT_SQL
fi

if [ ! -z "$redosize" ]; then
rsize=$redosize"M"
else
rsize=500"M"
fi

if [ ! -z "$characterset" ]; then
charset=`echo $characterset | tr '[a-z]' '[A-Z]'`
else
charset="AL32UTF8"
fi

echo "SET FEEDBACK 1\nSET NUMWIDTH 10\nSET LINESIZE 80\nSET TRIMSPOOL ON\nSET TAB OFF\nSET PAGESIZE 100\n" >> $CLONE_CRT_SQL
echo "CREATE CONTROLFILE SET DATABASE $newdb_name RESETLOGS" >> $CLONE_CRT_SQL
echo "MAXLOGFILES 32\nMAXLOGMEMBERS 2\nMAXDATAFILES 10000\nMAXINSTANCES 1\nMAXLOGHISTORY 800" >> $CLONE_CRT_SQL
echo "LOGFILE\nGROUP 1 '$BASEDIR/$SID/redo1.log' SIZE $rsize,\nGROUP 2 '$BASEDIR/$SID/redo2.log' SIZE $rsize,\nGROUP 3 '$BASEDIR/$SID/redo3.log' SIZE $rsize" >> $CLONE_CRT_SQL
echo "DATAFILE" >> $CLONE_CRT_SQL

for f in $FILES
do
   if [ -z "$DATAFILES" ]; then
      DATAFILES=${DATAFILES}"'"${f}"'"
   else 
      DATAFILES=${DATAFILES}",\n'"${f}"'"
   fi
done

echo "$DATAFILES" >> $CLONE_CRT_SQL
echo "CHARACTER SET $charset;\n" >> $CLONE_CRT_SQL
#echo "exit\n" >> $CLONE_CRT_SQL

exit 0

